package com.time.company.components.navigation.impl;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.time.company.R;
import com.time.company.utils.ScreenUtils;

/**
 * 默认导航条
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public class DefaultNavigation extends AbsNavigation<DefaultNavigation.Builder.DefaultNavigationParams> {
    protected DefaultNavigation(DefaultNavigation.Builder.DefaultNavigationParams params) {
        super(params);
    }

    @Override
    public int getLayoutId() {
        return R.layout.navigation_default;
    }

    @Override
    public void createAndBind() {
        super.createAndBind();
        setImage(R.id.iv_left, getParams().leftImageRes, getParams().leftOnClickListener);
        setText(R.id.tv_center, getParams().centerTextColor, getParams().centerText, getParams().centerOnClickListener);
    }

    protected void setText(int viewId, int text, View.OnClickListener onClickListener) {
        setText(viewId, 0, getString(text), onClickListener);
    }

    protected void setText(int viewId, String text, View.OnClickListener onClickListener) {
        setText(viewId, 0, text, onClickListener);
    }

    protected void setText(int viewId, int textColor, int text, View.OnClickListener onClickListener) {
        setText(viewId, textColor, getString(text), onClickListener);
    }

    protected void setText(int viewId, int textColor, String text, View.OnClickListener onClickListener) {
        View view = findViewById(viewId);
        if (view == null && !(view instanceof TextView)) {
            return;
        }
        TextView textView = (TextView) view;
        if (TextUtils.isEmpty(text)) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(TextUtils.ellipsize(text,
                    textView.getPaint(),
                    ScreenUtils.getDisplayWidth() * 2 / 3,
                    TextUtils.TruncateAt.END));
            textView.setOnClickListener(onClickListener);

            if (textColor > 0) {
                textView.setTextColor(getColor(textColor));
            }
        }

    }

    protected void setImage(int viewId, int leftImageRes, View.OnClickListener onClickListener) {
        View view = findViewById(viewId);
        if (view == null && !(view instanceof ImageView)) {
            return;
        }
        ImageView imageView = (ImageView) view;
        if (leftImageRes == 0) {
            imageView.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(leftImageRes);
            imageView.setOnClickListener(onClickListener);
        }
    }

    public static class Builder extends AbsNavigation.Builder {

        // 参数集
        protected DefaultNavigationParams navigationParams;

        public Builder(Context context, ViewGroup parent) {
            navigationParams = bindNavigationParams(context, parent);
        }

        protected DefaultNavigationParams bindNavigationParams(Context context, ViewGroup parent) {
            return new DefaultNavigationParams(context, parent);
        }

        public Builder setCenterText(String centerText) {
            navigationParams.centerText = centerText;
            return this;
        }

        public Builder setLeftImageRes(int leftImageRes) {
            navigationParams.leftImageRes = leftImageRes;
            return this;
        }

        public Builder setCenterText(int centerText) {
            navigationParams.centerText = navigationParams.context.getResources().getString(centerText);
            return this;
        }

        public Builder setLeftTextColor(int leftTextColor) {
            navigationParams.leftTextColor = leftTextColor;
            return this;
        }

        public Builder setCenterTextColor(int centerTextColor) {
            navigationParams.centerTextColor = centerTextColor;
            return this;
        }

        public Builder setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
            navigationParams.leftOnClickListener = leftOnClickListener;
            return this;
        }

        public Builder setCenterOnClickListener(View.OnClickListener centerOnClickListener) {
            navigationParams.centerOnClickListener = centerOnClickListener;
            return this;
        }

        @Override
        public DefaultNavigation create() {
            DefaultNavigation defaultNavigation = new DefaultNavigation(navigationParams);
            return defaultNavigation;
        }

        public static class DefaultNavigationParams extends AbsNavigation.Builder.NavigationParams {
            public int leftImageRes;
            public String centerText;
            public int leftTextColor;
            public int centerTextColor;

            public View.OnClickListener leftOnClickListener;
            public View.OnClickListener centerOnClickListener;

            public DefaultNavigationParams(Context context, ViewGroup parent) {
                super(context, parent);
            }
        }
    }
}
